package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkSpaceSerciveImpl implements WorkSpaceService {
    @Autowired
    private WorkSpaceMapper workSpaceMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /*
     * 查询今日运营数据
     * */
    @Override
    public BusinessDataVO getData() {
        LocalDate now = LocalDate.now();
        LocalDateTime localBeginTime = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime localEndTime = LocalDateTime.of(now, LocalTime.MAX);
        //今日新增用户数
        Integer todayAddUser = userMapper.getnewuser(localBeginTime, localEndTime);
        if (todayAddUser == null) todayAddUser = 0;
        //订单总数：
        Integer Order_count = orderMapper.countEveryday(localBeginTime, localEndTime);
        if (Order_count == null) Order_count = 0;
        //有效订单总数
        Integer validOrder_count = orderMapper.getValidOrderCountEveryday(localBeginTime, localEndTime);
        if (validOrder_count == null) validOrder_count = 0;
        //订单完成率
        Double complete_count;
        if (validOrder_count != 0 && Order_count != 0) {
            complete_count = (double) validOrder_count / Order_count;
        } else {
            complete_count = 0.0;
        }
        //营业额
        Double INE_amount = orderMapper.getSumMoney(localBeginTime, localEndTime);
        if (INE_amount == null) INE_amount = 0.0;
        //平均客单价
        Double unitPrice;
        if (INE_amount != 0 && Order_count != 0) {
            unitPrice = INE_amount / Order_count;
        } else {
            unitPrice = 0.0;
        }

        return BusinessDataVO.builder()
                .newUsers(todayAddUser)
                .orderCompletionRate(complete_count)
                .turnover(INE_amount)
                .unitPrice(unitPrice)
                .validOrderCount(validOrder_count)
                .build();
    }

    /*
     * 查询套餐总览
     * */
    @Override
    public SetmealOverViewVO getOverviewSetmeal() {
        //停售的套餐
        Integer discontinued = setmealMapper.CountSetMealByStatus(0);
        //起售的套餐
        Integer sold = setmealMapper.CountSetMealByStatus(1);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
    /*
     * 查询菜品总览
     * */
    @Override
    public DishOverViewVO getOverviewDish() {
        //停售的菜品
        Integer discontinued = dishMapper.CountDishByStatus(0);
        //起售的菜品
        Integer sold = dishMapper.CountDishByStatus(1);

        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /*
     * 查询订单数据接口
     * */
    @Override
    public OrderOverViewVO getOrderCount() {
        LocalDate now = LocalDate.now();
        LocalDateTime localBeginTime = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime localEndTime = LocalDateTime.of(now, LocalTime.MAX);
        //查询全部订单：
        Integer allOrders = orderMapper.countEveryday(localBeginTime, localEndTime);
        if (allOrders == null) allOrders = 0;
        //查询已经取消的订单
        Integer cancelledOrders = orderMapper.getCountByStatus(6);
        if (cancelledOrders == null) cancelledOrders = 0;
        //查询已经完成的订单
        Integer completedOrders = orderMapper.getCountByStatus(5);
        if (completedOrders == null) completedOrders = 0;
        //查询待派送的订单
        Integer pendingOrders = orderMapper.getCountByStatus(3);
        if (pendingOrders == null) pendingOrders = 0;

        //查询待接单的订单
        Integer waitingOrders = orderMapper.getCountByStatus(2);
        if (waitingOrders == null) waitingOrders = 0;

        return OrderOverViewVO.builder().
                allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(pendingOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    @Override
    public BusinessDataVO getBunissesData(LocalDateTime localBeginTime, LocalDateTime localEndTime) {
        //今日新增用户数
        Integer todayAddUser = userMapper.getnewuser(localBeginTime, localEndTime);
        if (todayAddUser == null) todayAddUser = 0;
        //订单总数：
        Integer Order_count = orderMapper.countEveryday(localBeginTime, localEndTime);
        if (Order_count == null) Order_count = 0;
        //有效订单总数
        Integer validOrder_count = orderMapper.getValidOrderCountEveryday(localBeginTime, localEndTime);
        if (validOrder_count == null) validOrder_count = 0;
        //订单完成率
        Double complete_count;
        if (validOrder_count != 0 && Order_count != 0) {
            complete_count = (double) validOrder_count / Order_count;
        } else {
            complete_count = 0.0;
        }
        //营业额
        Double INE_amount = orderMapper.getSumMoney(localBeginTime, localEndTime);
        if (INE_amount == null) INE_amount = 0.0;
        //平均客单价
        Double unitPrice;
        if (INE_amount != 0 && Order_count != 0) {
            unitPrice = INE_amount / Order_count;
        } else {
            unitPrice = 0.0;
        }

        return BusinessDataVO.builder()
                .newUsers(todayAddUser)
                .orderCompletionRate(complete_count)
                .turnover(INE_amount)
                .unitPrice(unitPrice)
                .validOrderCount(validOrder_count)
                .build();
    }


}


